package com.cskaoyan.service.impl;

import com.cskaoyan.bean.vo.AftersaleVo;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.model.MarketAftersale;
import com.cskaoyan.model.MarketAftersaleExample;
import com.cskaoyan.model.MarketOrder;
import com.cskaoyan.model.MarketOrderGoods;
import com.cskaoyan.service.AfterSaleService;
import com.cskaoyan.service.OrderGoodsService;
import com.cskaoyan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Autowired
    MarketAftersaleMapper aftersaleMapper;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderGoodsService orderGoodsService;

    @Override
    public int applyAfterSale(int userId, AftersaleVo aftersaleVo) {
        HashMap<String, Object> map = new HashMap<>();
        Date current = new Date();
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();
        // 生成一个0-999之间的随机数，确保订单号在同毫秒内不重复
        int randomNum = ThreadLocalRandom.current().nextInt(1000);
        // 格式化时间戳为日期字符串，去除中间的符号以简化
        String dateStr = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date(timestamp));

        BigDecimal amount = aftersaleVo.getAmount();
        Integer orderId = aftersaleVo.getOrderId();
        Short type = aftersaleVo.getType();
        String[] pictures = aftersaleVo.getPictures();
        String reason = aftersaleVo.getReason();

        MarketAftersale build = MarketAftersale.builder()
                .aftersaleSn(dateStr)
                .amount(amount)
                .orderId(orderId)
                .userId(userId)
                .pictures(pictures)
                .reason(reason)
                .type(type)
                .status((short) 1)
                .addTime(current)
                .updateTime(current)
                .deleted(false)
                .build();

        int result1 = aftersaleMapper.insertSelective(build);
        int result2 = orderService.updateAftersaleStatus(orderId);
        if (result1 == 1 && result2 == 1) {
            return 1;
        }
        return 0;
    }

    @Override
    public Map<String, Object> detail(Integer orderId) {

        MarketAftersaleExample example = new MarketAftersaleExample();
        example.createCriteria().andOrderIdEqualTo(orderId)
                .andDeletedEqualTo(false);
        List<MarketAftersale> aftersales = aftersaleMapper.selectByExample(example);

        MarketOrder order = orderService.findById(orderId);

        List<MarketOrderGoods> orderGoods = orderGoodsService.findByOrderId(orderId);

        Map<String, Object> map = new HashMap<>();
        map.put("aftersale", aftersales != null && aftersales.size() == 1 ? aftersales.get(0) : null);
        map.put("order", order);
        map.put("orderGoods", orderGoods);
        return map;
    }
}
